/*
#          .,:,,,                                        .::,,,::.          
#        .::::,,;;,                                  .,;;:,,....:i:         
#        :i,.::::,;i:.      ....,,:::::::::,....   .;i:,.  ......;i.        
#        :;..:::;::::i;,,:::;:,,,,,,,,,,..,.,,:::iri:. .,:irsr:,.;i.        
#        ;;..,::::;;;;ri,,,.                    ..,,:;s1s1ssrr;,.;r,        
#        :;. ,::;ii;:,     . ...................     .;iirri;;;,,;i,        
#        ,i. .;ri:.   ... ............................  .,,:;:,,,;i:        
#        :s,.;r:... ....................................... .::;::s;        
#        ,1r::. .............,,,.,,:,,........................,;iir;        
#        ,s;...........     ..::.,;:,,.          ...............,;1s        
#       :i,..,.              .,:,,::,.          .......... .......;1,       
#      ir,....:rrssr;:,       ,,.,::.     .r5S9989398G95hr;. ....,.:s,      
#     ;r,..,s9855513XHAG3i   .,,,,,,,.  ,S931,.,,.;s;s&BHHA8s.,..,..:r:     
#    :r;..rGGh,  :SAG;;G@BS:.,,,,,,,,,.r83:      hHH1sXMBHHHM3..,,,,.ir.    
#   ,si,.1GS,   sBMAAX&MBMB5,,,,,,:,,.:&8       3@HXHBMBHBBH#X,.,,,,,,rr    
#   ;1:,,SH:   .A@&&B#&8H#BS,,,,,,,,,.,5XS,     3@MHABM&59M#As..,,,,:,is,   
#  .rr,,,;9&1   hBHHBB&8AMGr,,,,,,,,,,,:h&&9s;   r9&BMHBHMB9:  . .,,,,;ri.  
#  :1:....:5&XSi;r8BMBHHA9r:,......,,,,:ii19GG88899XHHH&GSr.      ...,:rs.  
#  ;s.     .:sS8G8GG889hi.        ....,,:;:,.:irssrriii:,.        ...,,i1,  
#  ;1,         ..,....,,isssi;,        .,,.                      ....,.i1,  
#  ;h:               i9HHBMBBHAX9:         .                     ...,,,rs,  
#  ,1i..            :A#MBBBBMHB #  .r1,..        ,..;3BMBBBHBB#Bh.     ..                    ....,,,,,i1;   
#   :h;..       .,..;,1XBMMMMBXs,.,, .. :: ,.               ....,,,,,,ss.   
#    ih: ..    .;;;, ;;:s58A3i,..    ,. ,.:,,.             ...,,,,,:,s1,    
#    .s1,....   .,;sh,  ,iSAXs;.    ,.  ,,.i85            ...,,,,,,:i1;     
#     .rh: ...     rXG9XBBM#M#MHAX3hss13&&HHXr         .....,,,,,,,ih;      
#      .s5: .....    i598X&&A&AAAAAA&XG851r:       ........,,,,:,,sh;       
#      . ihr, ...  .         ..                    ........,,,,,;11:.       
#         ,s1i. ...  ..,,,..,,,.,,.,,.,..       ........,,.,,.;s5i.         
#          .:s1r,......................       ..............;shs,           
#          . .:shr:.  ....                 ..............,ishs.             
#              .,issr;,... ...........................,is1s;.               
#                 .,is1si;:,....................,:;ir1sr;,                  
#                    ..:isssssrrii;::::::;;iirsssssr;:..                    
#                         .,::iiirsssssssssrri;;:.
*/
package com.itheima.health.controller;

import com.itheima.health.common.EncodingIdConst;
import com.itheima.health.common.MessageConst;
import com.itheima.health.config.MyPassEncoderFactories;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <dl>
 * <dd>描述: ~ UserController</dd>
 * <dd>创建时间：  11:03 2021/2/23</dd>
 * <dd>创建人： guodong</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2021/2/23      guodong         1.0        1.0 Version
 * </pre>
 * </dl>
 */
@RestController
@RequestMapping("user")
@CrossOrigin
@Slf4j
public class UserController {

	@DubboReference
	private UserService userService;

	/**
	 * @Author: wty
	 * @Date: 17:06 2021/3/12
	 * @Parms [id]
	 * @ReturnType: com.itheima.health.entity.Result
	 * @Description: 根据用户id查询其->角色 ->权限->菜单

	 */
	@GetMapping("findMenuIdsById")
	public Result findMenuIdsById(Integer id) {
		log.info("[<----------根据用户id查询其->角色 ->权限->菜单]id:{}",id);
		List<Integer> menuIds = userService.findMenuIdsById(id);
		return new Result(true, "",menuIds);

	}

	/**
	 * @Author: wty
	 * @Date: 11:26 2021/3/12
	 * @Parms [rolesId, user]
	 * @ReturnType: com.itheima.health.entity.Result
	 * @Description: 编辑提交户
	 */
	@PostMapping("edit")
	@PreAuthorize("hasAnyAuthority('USER_EDIT')")
	public Result edit(Integer[] RolesIds,  @RequestBody com.itheima.health.pojo.User user) {
		PasswordEncoder myPasswordEncoder = MyPassEncoderFactories.createMyPasswordEncoder(EncodingIdConst.BCRYPT);
		user.setPassword(myPasswordEncoder.encode(user.getPassword()));
		userService.edit(RolesIds,user);
		return new Result(true, "编辑成功");
	}

	/**
	 * @Author: wty
	 * @Date: 10:51 2021/3/12
	 * @Parms [id]
	 * @ReturnType: com.itheima.health.entity.Result
	 * @Description:
	 */
	@GetMapping("findRolesIdsById")
	@PreAuthorize("hasAnyAuthority('USER_QUERY')")
	public Result findRolesIdsById(Integer id) {

		List<Integer> RolesIds = userService.findRolesIdsById(id);
		return new Result(true, "", RolesIds);

	}

	/**
	 * @Author: wty
	 * @Date: 10:43 2021/3/12
	 * @Parms [id]
	 * @ReturnType: com.itheima.health.entity.Result
	 * @Description: 用户编辑回显
	 */
	@GetMapping("findById")
	@PreAuthorize("hasAnyAuthority('USER_QUERY')")
	public Result findById(Integer id) {
		com.itheima.health.pojo.User user = userService.findById(id);
		return new Result(true, "", user);
	}

	/**
	 * @Author: Administrator
	 * @Date: 18:08 2021/3/11
	 * @Parms [id]
	 * @ReturnType: com.itheima.health.entity.Result
	 * @Description: 删除用户
	 */
	@GetMapping("/delete")
	@PreAuthorize("hasAnyAuthority('USER_DELETE')")
	public Result delete(Integer id) {
		log.info("[删除----前端id参数]data:{}", id);
		userService.delete(id);
		return new Result(true, "删除用户成功");
	}

	/**
	 * @Author: wty
	 * @Date: 18:08 2021/3/11
	 * @Parms [RolesIds, user]
	 * @ReturnType: com.itheima.health.entity.Result
	 * @Description: 添加用户
	 */
	@PostMapping("add")
	@PreAuthorize("hasAnyAuthority('USER_ADD')")
	//@PreAuthorize("hasAnyAuthority('USER_ADD')")
	public Result add(Integer[] RolesIds, @RequestBody com.itheima.health.pojo.User user) {
		log.info("[前端RolesIds==]RolesIds:{}", RolesIds);
		PasswordEncoder myPasswordEncoder = MyPassEncoderFactories.createMyPasswordEncoder(EncodingIdConst.BCRYPT);
		user.setPassword(myPasswordEncoder.encode(user.getPassword()));
		userService.add(RolesIds, user);
		return new Result(true, "添加成功");
	}

	/**
	 * @Author: Administrator
	 * @Date: 10:35 2021/3/11
	 * @Parms [queryPageBean]
	 * @ReturnType: com.itheima.health.entity.Result
	 * @Description: 用户数据查询
	 */
	@GetMapping("findPage")
	@PreAuthorize("hasAnyAuthority('USER_QUERY')")
	//@PreAuthorize("hasAnyAuthority('USER_QUERY')")
	public Result findPage(QueryPageBean queryPageBean) {
		PageResult pageResult = userService.findPage(queryPageBean);
		return new Result(true, "", pageResult);
	}
	/**
	 * @Author: guodong
	 * @Date: 11:13 2021/2/23
	 * @Parms [loginParam]
	 * @ReturnType: com.itheima.health.entity.Result
	 * @Description: 用户登录
	 */
   /* @PostMapping("login")
    public Result login(@RequestBody LoginParam loginParam){
        //1.根据用户名查询用户
        User user = userService.findUserByName(loginParam.getUsername());
        //2.判断用户是否为null 或者 用户密码是否一致 如果两者有其一不确立那么返回登录失败
        if(null == user || !user.getPassword().equals(loginParam.getPassword())){
            //return new Result(false, "用户名或密码错误");
            return new Result(false, MessageConst.LOGIN_FAIL);
        }
        //3.登录成功
        return new Result(true, MessageConst.LOGIN_SUCCESS);
    }*/

	/**
	 * @Author: guodong
	 * @Date: 9:12 2021/3/8
	 * @ClassName: UserController
	 * @Parms []
	 * @ReturnType: com.itheima.health.entity.Result
	 * @Description: 登录成功后的请求地址
	 */
	@PostMapping("loginSuccess")
	public Result loginSuccess() {
		return new Result(true, MessageConst.LOGIN_SUCCESS);
	}

	/**
	 * @Author: guodong
	 * @Date: 9:12 2021/3/8
	 * @ClassName: UserController
	 * @Parms []
	 * @ReturnType: com.itheima.health.entity.Result
	 * @Description: 登录失败后的请求地址
	 */
	@PostMapping("loginFail")
	public Result loginFail() {
		return new Result(false, MessageConst.LOGIN_FAIL);
	}

	/**
	 * @Author: guodong
	 * @Date: 10:07 2021/3/8
	 * @ClassName: UserController
	 * @Parms []
	 * @ReturnType: com.itheima.health.entity.Result
	 * @Description: 获取用户名称
	 */
	@GetMapping("getUserName")
	public Result getUserName() {
		Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if (null != obj && obj instanceof User) {
			//注意此处的user是security包下面的，不是我们的pojo下面的实体
			User user = (User) obj;
			String username = user.getUsername();
			return new Result(true, MessageConst.GET_USERNAME_SUCCESS, username);
		}
		return new Result(false, MessageConst.GET_USERNAME_FAIL);
	}
}
